import _regeneratorRuntime from "@babel/runtime/regenerator";
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator";
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import _inherits from "@babel/runtime/helpers/esm/inherits";
import _createSuper from "@babel/runtime/helpers/esm/createSuper";
import _isString from "lodash/isString";
import _isArray from "lodash/isArray";
import React from 'react';
import { connect } from 'dva';
import { getCurrentOrganizationId } from "../utils";
import intl from '.';
import { queryPromptLocale } from "../../services/api";
var cache = window.intlCache || (window.intlCache = new Map());

function getDisplayName(WrappedComponent) {
  return WrappedComponent.displayName || WrappedComponent.name || 'Component';
}

export default function formatterCollections() {
  var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
      _ref$code = _ref.code,
      code = _ref$code === void 0 ? '' : _ref$code;

  return function (Component) {
    var intlLoadPromise = function intlLoadPromise(orgId, lang, promptKey) {
      return queryPromptLocale(orgId, lang, promptKey);
    };

    var IntlComponent = /*#__PURE__*/function (_React$Component) {
      _inherits(IntlComponent, _React$Component);

      var _super = _createSuper(IntlComponent);

      function IntlComponent() {
        var _this;

        _classCallCheck(this, IntlComponent);

        for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
          args[_key] = arguments[_key];
        }

        _this = _super.call.apply(_super, [this].concat(args));
        _this.state = {
          localeLoaded: false
        };
        _this.loading = false;
        _this.currentLanguage = null;
        return _this;
      }

      _createClass(IntlComponent, [{
        key: "loadLocale",
        value: function () {
          var _loadLocale = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(language) {
            var currentCacheLang, promptKey, multipleCode, organizationId, data;
            return _regeneratorRuntime.wrap(function _callee$(_context) {
              while (1) {
                switch (_context.prev = _context.next) {
                  case 0:
                    if (!(language && this.currentLanguage !== language)) {
                      _context.next = 24;
                      break;
                    }

                    // 必须要 language 有值
                    this.currentLanguage = language;

                    if (cache.has(language)) {
                      currentCacheLang = cache.get(language);
                    } else {
                      currentCacheLang = new Map();
                      cache.set(language, currentCacheLang);
                    }

                    promptKey = '';
                    multipleCode = [];

                    if (_isString(code)) {
                      if (!currentCacheLang.get(code)) {
                        promptKey = code;
                        currentCacheLang.set(code, true);
                      }
                    } else if (_isArray(code)) {
                      code.forEach(function (c) {
                        if (!currentCacheLang.get(c)) {
                          currentCacheLang.set(c, true);
                          multipleCode.push(c);
                        }
                      });
                      promptKey = multipleCode.join(',');
                    }

                    if (!promptKey) {
                      _context.next = 23;
                      break;
                    }

                    this.loading = true;
                    _context.prev = 8;
                    organizationId = getCurrentOrganizationId() || 0;
                    _context.next = 12;
                    return intlLoadPromise(organizationId, language, promptKey);

                  case 12:
                    data = _context.sent;

                    if (!data) {
                      _context.next = 16;
                      break;
                    }

                    // if (isArray(code)) {
                    //   code.forEach(c => {
                    //     currentCacheLang.set(c, data);
                    //   });
                    // } else {
                    //   currentCacheLang.set(code, data);
                    // }
                    intl.load(_defineProperty({}, language, data));
                    return _context.abrupt("return");

                  case 16:
                    // 语言没有加载成功
                    if (_isArray(code)) {
                      code.forEach(function (c) {
                        currentCacheLang.delete(c);
                      });
                    } else {
                      currentCacheLang.delete(code);
                    }

                  case 17:
                    _context.prev = 17;
                    this.loading = false;
                    this.setState({
                      localeLoaded: true
                    });
                    return _context.finish(17);

                  case 21:
                    _context.next = 24;
                    break;

                  case 23:
                    this.setState({
                      localeLoaded: true
                    });

                  case 24:
                  case "end":
                    return _context.stop();
                }
              }
            }, _callee, this, [[8,, 17, 21]]);
          }));

          function loadLocale(_x) {
            return _loadLocale.apply(this, arguments);
          }

          return loadLocale;
        }()
      }, {
        key: "componentDidMount",
        value: function componentDidMount() {
          var language = this.props.language;
          this.loadLocale(language);
        }
      }, {
        key: "componentDidUpdate",
        value: function componentDidUpdate() {
          this.loadLocale(this.props.language);
        }
      }, {
        key: "shouldComponentUpdate",
        value: function shouldComponentUpdate() {
          return !this.loading;
        }
      }, {
        key: "render",
        value: function render() {
          var localeLoaded = this.state.localeLoaded;
          return localeLoaded ? /*#__PURE__*/React.createElement(Component, Object.assign({
            intl: intl
          }, this.props)) : null;
        }
      }]);

      return IntlComponent;
    }(React.Component);

    IntlComponent.displayName = "IntlComponent(".concat(getDisplayName(Component), ")");
    return connect(function (_ref2) {
      var _ref2$global = _ref2.global,
          global = _ref2$global === void 0 ? {} : _ref2$global;
      return {
        language: global.language
      };
    })(IntlComponent);
  };
}